package com.zr.planClass.controller;

import com.zr.enumpackage.StatusEnum;
import com.zr.planClass.model.*;
import com.zr.planClass.service.PlanClassService;
import com.zr.util.ErrorMessageClass;
import com.zr.util.ErrorMessageEnum;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hasee on 2019/12/20.
 */
@RestController
@CrossOrigin//支持跨域
public class PlanClassController {

    @Autowired
    private PlanClassService planClassService;

    @PostMapping("planCategory/add")
    public ResultVo addPlanClass(@RequestBody @Valid PlanClassAddVo planClassAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        List<PlanClassAddVo> planClassAddVoList = new ArrayList<>();
        planClassAddVoList.add(planClassAddVo);
        ResultVo resultVo =  planClassService.add(planClassAddVoList);
        return resultVo;
    }
    @GetMapping("see")
    public ResultVo queryById(Integer id){
        if (id==null){
            return ResultBuilderVo.error("查看时id不能为空！");
        }
        ResultVo resultVo =  planClassService.queryById(id);
        return resultVo;
    }
    @PostMapping("planCategory/update")
    public ResultVo updatePlanClass(@RequestBody @Valid PlanClassUpdateVo planClassUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo =  planClassService.update(planClassUpdateVo);
        return resultVo;
    }
    @PostMapping("planCategory/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid PlanClassUpdateStatusVo planClassUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo =  planClassService.updateStatus(planClassUpdateStatusVo);
        return resultVo;
    }

    @PostMapping("planCategory/queryPage")
    public ResultVo queryPage(@RequestBody PlanClassSelectVo planClassSelectVo){
        ResultVo resultVo =  planClassService.queryPage(planClassSelectVo);
        return resultVo;
    }
    @PostMapping("metadata/getPullDownList")
    public ResultVo xiala(){
       /* List<StatusVo> statusVoList = new ArrayList<>();
        StatusVo statusVo = new StatusVo();
        statusVo.setLabel(StatusEnum.JINYONG.getName());
        statusVo.setValue(StatusEnum.JINYONG.getValue());
        statusVoList.add(statusVo);
        StatusVo statusVo2 = new StatusVo();
        statusVo2.setLabel(StatusEnum.qiyong.getName());
        statusVo2.setValue(StatusEnum.qiyong.getValue());
        statusVoList.add(statusVo2);*/
        List<StatusVo> statusVoList = new ArrayList<>();
        for (StatusEnum c : StatusEnum.values()) {
            StatusVo statusVo2 = new StatusVo(c.getName(),c.getValue());
            statusVoList.add(statusVo2);
        }
        return ResultBuilderVo.success(statusVoList);
    }

    @PostMapping("importPlanClass")
    public ResultVo importPlanClass(MultipartFile file)throws Exception{
       return planClassService.importPlanClass(file);
    }

}
